import path from 'node:path'
import vue from '@vitejs/plugin-vue'
import UnoCSS from 'unocss/vite'
import { defineConfig, loadEnv } from 'vite'
import babel from 'vite-plugin-babel'
import { name as packageName } from './package.json'

// https://vitejs.dev/config/

export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd())

  return defineConfig({
    base: env.VITE_BASE_URL,
    plugins: [
      babel(),
      vue(),
      UnoCSS(),
    ],
    server: {
      open: true,
      port: Number(env.VITE_APP_PORT),
      proxy: {
        // 跨栏后台
        [env.VITE_APP_API_BASE_URL]: {
          target: env.VITE_APP_HOST_URL,
          changeOrigin: true,
        },
      },
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    build: {
      target: 'es2015',
      outDir: packageName,
    },
  })
}
